#自动取款机的存取款模拟效果
#1.登录系统
#2.查询余额
#3.取钱
#4.存钱操作
#5.退出系统
import sys
# 账号信息列表
account = [{'ID':'20200501','name':'tom','passwd':'127756','balance':10.00},
              {'ID':'20200502','name':'jerry','passwd':'652331','balance':100.00},
              {'ID':'20200503','name':'lisa','passwd':'235213','balance':2000.00},
              {'ID':'20200504','name':'jennie','passwd':'985243','balance':1000.00}]

#供管理员查看账号信息
def show_stus_info():
    if len(account)==0:
        print("="*20,"无账户信息","="*20)
        return
    print('|{0:<5}||{1:<10}||{2:<8}||{3:<12}|'.format("ID","name","passwd","balance"))
    print("-"*40)
    for i,account_dict in enumerate(account):#enumerate枚举 把列表里面每个元素和索引拼在一起
        print('|{0:<5}||{1:<10}||{2:<8}||{3:<12}|'.format(i+1,account_dict["name"], account_dict["passwd"],account_dict["balance"]))
show_stus_info()


#登录
def login_in(name,password):
    for i in range(len(account)):
        if name in account[i]["name"]:
            if account[i]["passwd"] == password:
                print("登录成功")
                return i
            elif account[i]["passwd"] != password:
                print("密码错误，请重试")
                return -1
        else:
            continue
    if name not in account:
        print('无此用户')
#4.退出系统
def loginout():
    pass#占位

#登录后 ATM可以实现的操作
def ATM_operation(index):
    key=int(input("请选择您要进行的操作："))
    if key==1:#查看余额
        print("用户余额：",account[index]["balance"])
    elif key==2:#取钱操作
        input_cash=float(input("输入要取出金额"))
        if input_cash<=account[index]["balance"]:
            account[index]["balance"]=account[index]["balance"]-input_cash
            print("已取出",input_cash,"您当前余额为",account[index]["balance"])
        else:
            print("余额不足")
    elif key==3:#存钱操作
        input_cash = float(input("输入要存入金额"))
        account[index]["balance"] = account[index]["balance"] + input_cash
        print("已存入", input_cash, "您当前余额为", account[index]["balance"])
    elif key==4:#退出登录
        loginout()
        print("=" * 12, "再见", "=" * 12)
        sys.exit()


#界面交互
print("="*12,"自助存取款机","="*12)
print("请先登录")
name = input("请输入你的用户名：")
password = input("请输入密码：")
index_account=login_in(name,password)
if index_account+1>0:
    while True:
        print("{:1}{:11}{:15}".format(" ","1.查询余额","2.取钱"))
        print("{:1}{:13}{:15}".format(" ", "3.存钱", "4.退出系统"))
        print("="*40)
        ATM_operation(index_account)
        input("按回车键继续：")
elif index_account==-1:
    print("登录失败，请检查用户名或密码")
    sys.exit()